#include "testdialog.h"
#include "ui_testdialog.h"

testDialog::testDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::testDialog)
{
    ui->setupUi(this);
    ui->radioButtonHide->hide();
    //choose paper number
    ChoosePaper *paper=new ChoosePaper();
    paper->setModal(true);
    paper->move((QApplication::desktop()->width()- paper->width())/2, (QApplication::desktop()->height() - paper->height())/2);
    int retpaperID=paper->exec();
    m_TestPaperID=retpaperID;
    qDebug()<<"retpaperID"<<retpaperID;
    //if (retpaperID<=0)
        //return;
    ////////////////
    QString sql=QString("select ID,testid,itemanswer from testpaper_item where testpaperid=%1").arg(retpaperID);
    qDebug()<<sql;
    m_testquery=new QSqlQuery(sql);
    m_testquery->last();
    m_iTestAmount=m_testquery->at()+1;
    qDebug()<<"m_iTestAmount"<<m_iTestAmount;
    m_bScrollBarStoped=true;
    m_testquery->first();
    m_iCurrentTestpaperitemID=m_testquery->value(0).toInt();
    m_iCurrentTestID=m_testquery->value(1).toInt();
    this->ShowQuestion(m_iCurrentTestID);
    this->ShowItemAnswer();
    ui->horizontalScrollBar->setMaximum(m_iTestAmount);
    ui->lblNumber->setText(QString("%1").arg(1));


}

testDialog::~testDialog()
{
    delete ui;
}

void testDialog::drawTestDoc()
{
    /* remote image is loading after ..... */
    if (Otest) {
        ui->textEdit->setDocument ( Otest->document()->clone() );
        delete Otest;
        }

        if (forceQuestion_test) {
            forceQuestion_test->quit();
        }
}

void testDialog::on_horizontalScrollBar_valueChanged(int i)
{
   // this->ShowQuestion(ui->horizontalScrollBar->value());
   // m_query->seek()
    ui->lblNumber->setText(QString("%1").arg(i));

   if (!m_bScrollBarStoped)
        return;

    if (m_testquery->seek(i-1,false))
    {
        m_iCurrentTestpaperitemID=m_testquery->value(0).toInt();
        m_iCurrentTestID=m_testquery->value(1).toInt();
        ShowQuestion(m_iCurrentTestID);
        ShowItemAnswer();
     }
    qDebug()<<"value changed"<<m_iCurrentTestpaperitemID;
}




void testDialog::on_horizontalScrollBar_sliderPressed()
{
    m_bScrollBarStoped=false;

}
void testDialog::on_horizontalScrollBar_sliderReleased()
{
    m_bScrollBarStoped=true;
    int i=ui->horizontalScrollBar->value();
    on_horizontalScrollBar_valueChanged(i);
}

void testDialog::ShowQuestion(int ShowID)
{
    QSqlQuery query(QString("SELECT ID,QUESTION,OPTIONS,ANSWER,subjectID,teststyleID from test WHERE ID=%1")
                            .arg(ShowID));
    //qDebug()<<"qestionid"<<QuestionID;
    if(!query.exec())
    {
    qDebug()<<"[load error]";
    }
    else
    {
        if (query.next())
        {
                  QByteArray byteQuestion = query.value(1).toByteArray();
                  forceQuestion_test = new PushDoc(this);
                  Otest = new OOReader(byteQuestion);
                  Otest->moveToThread(forceQuestion_test);
                  connect(forceQuestion_test, SIGNAL(started()),Otest,SLOT(readbuffer()));
                  connect(Otest, SIGNAL(ready()), this, SLOT(drawTestDoc()));
                  //connect(Ooo, SIGNAL(statusRead(int,int)), this, SLOT(onRead(int,int)));
                  forceQuestion_test->start();
            }
    }
}

void testDialog::ShowItemAnswer()
{


    int iAnswer=m_testquery->value(2).toInt();
    qDebug()<<"iAnswer"<<iAnswer;

    switch(iAnswer)
    {
        case 1: ui->radioButtonA->setChecked(true);break;
        case 2: ui->radioButtonB->setChecked(true);break;
        case 3: ui->radioButtonC->setChecked(true);break;
        case 4: ui->radioButtonD->setChecked(true);break;
        case 5: ui->radioButtonE->setChecked(true);break;
    default:
        ui->radioButtonHide->setChecked(true);break;
    }

}


void testDialog::UpdateItemAnswer(int iAnswer)
{
    QSqlQuery query(QString("update testpaper_item set itemanswer=%1 WHERE ID=%2")
                                            .arg(iAnswer)
                                            .arg(m_iCurrentTestpaperitemID));
    qDebug()<<QString("update testpaper_item set itemanswer=%1 WHERE ID=%2")
              .arg(iAnswer)
              .arg(m_iCurrentTestpaperitemID);

    QSqlQuery query1(QString("update testpaper_item set isright=(select (case when a.answer=b.itemanswer then 1 else 0 end) as isrighta from testpaper_item b left join test a on b.testid=a.id where b.testpaperid=%1 and b.id=%2) where id=%2")
                    .arg(m_TestPaperID)
                    .arg(m_iCurrentTestpaperitemID));
    qDebug()<<QString("update testpaper_item set isright=(select (case when a.answer=b.itemanswer then 1 else 0 end) as isrighta from testpaper_item b left join test a on b.testid=a.id where b.testpaperid=%1 and b.id=%2) where id=%2")
              .arg(iAnswer)
              .arg(m_iCurrentTestpaperitemID);

}

void testDialog::on_radioButtonA_clicked(bool checked)
{
     if (checked)
     {
         UpdateItemAnswer(1);
         qDebug()<<"on_radioButtonA_toggled";
     }

}

void testDialog::on_radioButtonB_clicked(bool checked)
{
     if (checked)
     {
         UpdateItemAnswer(2);
         qDebug()<<"on_radioButtonB_toggled";
     }

}
void testDialog::on_radioButtonC_clicked(bool checked)
{
     if (checked)
     {
         UpdateItemAnswer(3);
         qDebug()<<"on_radioButtonC_toggled";
     }

}
void testDialog::on_radioButtonD_clicked(bool checked)
{
     if (checked)
     {
         UpdateItemAnswer(4);
         qDebug()<<"on_radioButtonD_toggled";
     }

}
void testDialog::on_radioButtonE_clicked(bool checked)
{
     if (checked)
     {
         UpdateItemAnswer(5);
         qDebug()<<"on_radioButtonE_toggled";
     }

}

void testDialog::on_ButtonHandin_clicked()
{
    QSqlQuery query(QString("update testpaper set score=(select round(SUM(ISRIGHT)*1.0/count(*)*100,2) as percent from testpaper_item where testpaperid=%1) where id=%1")
                                            .arg(m_TestPaperID));

    qDebug()<<QString("update testpaper set score=(select round(SUM(ISRIGHT)*1.0/count(*)*100,2) as percent from testpaper_item where testpaperid=%1) where id=%1")
              .arg(m_TestPaperID);
    this->accept();
}

void testDialog::on_ButtonQuit_clicked()
{
    QSqlQuery query1(QString("select round(count(itemanswer)*1.0/count(*)*100,2) as percent from testpaper_item where testpaperid=%1")
                    .arg(m_TestPaperID));
    query1.first();
    double  dPercent=query1.value(0).toDouble();
    qDebug()<<QString("select round(count(itemanswer)*1.0/count(*)*100,2) as percent from testpaper_item where testpaperid=%1")
              .arg(m_TestPaperID);
    qDebug()<<dPercent;
    QSqlQuery query2(QString("update testpaper set percent=%1 WHERE ID=%2")
                                            .arg(dPercent)
                                            .arg(m_TestPaperID));
    qDebug()<<QString("update testpaper set percent=%1 WHERE ID=%2")
              .arg(dPercent)
              .arg(m_TestPaperID);

      this->accept();

}
